{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 处理 txt 文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Honor time with civilization instead of giving civilization time\n",
      "给岁月以文明，而不是给文明以岁月\n",
      "\n"
     ]
    }
   ],
   "source": [
    "file = 'file.txt'\n",
    "f = open(file, 'r')\n",
    "# s1 = f.read(10) # 读取接下来的 N 个字符到字符串\n",
    "s = f.read() # 把整个文件读进一个字符串\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Honor time with civilization instead of giving civilization time\\n'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = open(file, 'r')\n",
    "s = f.readline() # 读取下一行\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Honor time with civilization instead of giving civilization time\\n',\n",
       " '给岁月以文明，而不是给文明以岁月\\n']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = open(file, 'r')\n",
    "s = f.readlines() # 读取整个文件到一个字符串列表\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "f.close() # 打开文件后要关闭文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Honor time with civilization instead of giving civilization time\n",
      "\n",
      "给岁月以文明，而不是给文明以岁月\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 从文本文件逐行读取的最佳方式就是根本不要读取该文件，文本也是一个迭代器，\n",
    "# 可以用for循环、列表推导，或者其它迭代上下文中自动逐行读取文件。\n",
    "# 文本迭代器往往是最佳选择\n",
    "for line in open(file):\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 处理 Python 原生对象 pickle[B1P293]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pickle 模块可以让我们直接在文件中存储几乎任何Python对象的高级工具，同时不需要我们对字符串进行来回转换。它就像是超级通用的数据格式化和解析工具。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "D = {'a':1, 'b':2}\n",
    "F = open('datafile.pkl', 'wb')\n",
    "import pickle\n",
    "pickle.dump(D, F)\n",
    "F.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 2}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取回刚才存储的数据，只需要再次使用pickle重建。\n",
    "F = open('datafile.pkl', 'rb')\n",
    "E = pickle.load(F)\n",
    "E"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 文件上下文管理器\n",
    "它把文件处理代码包装到一个逻辑层里，以确保在退出后一定会自动关闭文件（同时可以满足将其输出缓冲区内容写入磁盘），而不是依靠垃圾回收时的自动关闭。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Honor time with civilization instead of giving civilization time\n",
      "***\n",
      "给岁月以文明，而不是给文明以岁月\n",
      "***\n"
     ]
    }
   ],
   "source": [
    "with open('file.txt', 'r') as f:\n",
    "    for line in f:\n",
    "        print(line + '***')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
